package com.flyxteam.commons.httpclient;

import java.util.concurrent.TimeUnit;

import org.apache.http.conn.HttpClientConnectionManager;

/**
 * 空闲连接监视器
 * @author BZX
 *
 */
public class IdleConnectionMonitor {
	
	/**
	 * 空闲连接监视器线程
	 * @author BZX
	 *
	 */
	public static class IdleConnectionMonitorThread extends Thread {
	
	    private final HttpClientConnectionManager connMgr;
	    private volatile boolean shutdown;
	
	    public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
	        super();
	        this.connMgr = connMgr;
	    }
	
	    @Override
	    public void run() {
	        try {
	            while (!shutdown) {
	                synchronized (this) {
	                    wait(5000);
	                    // 关闭失效的连接
	                    connMgr.closeExpiredConnections();
	                    // 可选的, 关闭30秒内不活动的连接
	                    connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
	                }
	            }
	        } catch (InterruptedException ex) {
	            // terminate
	        }
	    }
	
	    public void shutdown() {
	        shutdown = true;
	        synchronized (this) {
	            notifyAll();
	        }
	    }
	
	}
}
